<?php
namespace app\index\controller;

use think\Controller;
use app\index\controller\Common;

class Article extends Common
{
    public function index()
    {
        $return = $this->doArticle();
        return $this->fetch($return['showcate']==1?'index@'.config('webthemes').'/list'.$return['tpl']:'index@'.config('webthemes').'/show'.$return['tpl']);
    }
    
    /***列表处理方法
    * @params type 栏目英文名,p 当前页,mbispc 0 电脑端 1 移动端
    ***/
    public function doArticle($type="", $p=1, $mbispc=0)
    {
        $q_nid   =  getNID();
        if ($type!="") {
            $q_nid = $type;
        }
        $q_cid   = 0;
        $title   = urldecode(input('title'));
        $cid     = "";
        $pagenum = 12; //分页数量
        if ($q_nid!="") {
            $map['entitle']	= $q_nid;
            $nid = db('nav')->where($map)->value("id");
            if ($nid=="") {
                $res1 = db('cate')->field('nid,id')->where($map)->find();
                $nid  = $res1['nid'];
                $cid  = $res1['id'];
                $q_cid = 1;	//是分类
            }
        }
        if ($nid=="") { abort(404,'栏目ID或分类ID不存在!'); }
        $navrow = db('nav')->where('id', $nid)->find();
        $data["nid"] 	 = $navrow['id'];
				$banner = $navrow['img'];
				if(config('webmstatus')==1 && isMobile()){		$banner = $navrow['img1']; }
        $data["banner"]	 = $banner;
        $data["columnName"]= $navrow['title'];
        $data["showcate"]	 = $navrow['showcate'];
        $data["entitle"] = $navrow['entitle'];
        $navrow['page'] && $pagenum = $navrow['page'];
        //无分类直接显示文章
        if ($navrow['showcate']==0) {
            $data["leftlist"] = db('article')->field('id,title')->where('nid', $nid)->order("sort,id")->select();
            $id  = db('article')->where('nid', $nid)->order('sort,id')->value('id');
            $data['one'] = db('article')->where('id', $id)->find();
						$data['one']['content']  = pregReplaceImgurl($data['one']['content'],substr(config('weburl'),0,-1)); //补全图片路径
						$data['one']['content1'] = pregReplaceImgurl($data['one']['content1'],substr(config('weburl'),0,-1));
            $data['id'] = $id;
        }
        //有分类直接显示列表
        if ($navrow['showcate']==1) {
            $where['nid'] = $nid;
            $data["leftlist"] = webtreelist('cate', 'title,img,id,entitle,content', array('nid'=>$nid));
            if ($q_cid==1 && $cid != "") {
                $where['cid'] = array("in",webtreecatewhere($cid));
                $data["cid"] = $cid;
                $data['cate'] = db('cate')->where('id', $cid)->find();
                $data['cate']['page'] && $pagenum = $data['cate']['page'];
            }
            if (!config('weburltype')) {
                $p = intval(input('p'))==0?1:intval(input('p'));
            }
            if ($title!="") {
                $where['title'] = array('like','%'.$title.'%');
                $params['title'] = $title;
            }
            $order = "istop desc,sort desc,id desc";
            $list  = db('article')->where($where)->order($order)->limit(($p-1)*$pagenum, $pagenum)->select();
            $count = db('article')->where($where)->count();
            $page = new \app\common\org\Page($count, $pagenum, $q_nid, $p, $params, $mbispc);
            $data["list"] = $list;
            $data["page"] = $page->show();
        }
        $data["site"] = getseo($nid, "", $cid);
        $data["cateName"] = getCateName($cid, $nid);
        $this->assign($data);
          
        //模板定义
        $ntpl  = $navrow['showcate']==1?$navrow['list_tpl']:$navrow['msg_tpl'];
        $mtpl = db('module')->where('id', $navrow['mid'])->value('tpl');
        $tpl  = $ntpl ? $ntpl : $mtpl;
        $return['tpl'] = $tpl;
        $return['showcate'] = $navrow['showcate'];
        $return['iscate'] = $q_cid;
        return $return;
    }
}
